package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.TLaser;
import com.tmeone.erpone.entity.TOrder;
import com.tmeone.erpone.entity.TPrinter;
import com.tmeone.erpone.service.ITLaserService;
import com.tmeone.erpone.service.ITOrderService;
import com.tmeone.erpone.service.ITPrinterService;
import com.tmeone.erpone.util.OrderIdsAndPrinterNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyg
 * @since 2021-04-20
 */
@RestController
public class TLaserController {
    @Autowired
    private ITLaserService laserService;

    @Autowired
    private ITOrderService orderService;

    @Autowired
    private ITPrinterService printerService;

    //根据编号模糊查询并分页查询
    @GetMapping("/lasers/search")
    public JsonResult getLaserByoid(@RequestParam(value = "search",required = false,defaultValue = "") String search,
                                    @RequestParam(value = "current",required = false,defaultValue = "1") int current,
                                    @RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize){
        Page page = new Page(current,pageSize);
        return new JsonResult(1,laserService.findOrderByoid(page,search));
    }

    //根据订单编号删除激光码
    @PostMapping("/lasers/deletes")
    public JsonResult deleteLasers(@RequestBody List<String> orderids){
        QueryWrapper<TLaser> queryWrapperLaser = new QueryWrapper<>();
        UpdateWrapper<TOrder> updateWrapperOrder = new UpdateWrapper<>();

        for (String orderid : orderids){
            queryWrapperLaser.eq("l_no",orderid);
            queryWrapperLaser.or();

            updateWrapperOrder.eq("order_id",orderid);
            updateWrapperOrder.or();
            updateWrapperOrder.set("laser_code",-1);
        }
        laserService.remove(queryWrapperLaser);
        orderService.update(updateWrapperOrder);

        return new JsonResult(1,"批量删除成功");
    }

    //批量添加激光码
    @PostMapping("/lasers/lasercode")
    public JsonResult saveLasers(@RequestBody List<String> orderids){
        UpdateWrapper<TOrder> updateWrapperOrder = new UpdateWrapper<>();

        updateWrapperOrder.set("laser_code",1);

        List<TLaser> lasers = new ArrayList<>();
        for (String orderid : orderids){
            TLaser laser = new TLaser();
            laser.setId(UUID.randomUUID().toString().substring(0,32));
            laser.setLNo(orderid);
            laser.setLStatus("-1");
            laser.setLCode(UUID.randomUUID().toString().substring(0,24));
            lasers.add(laser);

            updateWrapperOrder.eq("order_id",orderid);
            updateWrapperOrder.or();
        }


        laserService.saveBatch(lasers);
        orderService.update(updateWrapperOrder);
        return new JsonResult(1,"激光码生成成功");
    }

    //单表，激光码打印页面查询
    @GetMapping("/lasers/printer")
    public JsonResult getLasersAndPrinter(@RequestParam(value = "radio",required = false,defaultValue = "") String radio,
                                          @RequestParam(value = "search",required = false,defaultValue = "") String search,
                                          @RequestParam(value = "current",required = false,defaultValue = "1") int current,
                                          @RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize){
        Page page = new Page(current,pageSize);
        System.out.println(radio);
        return new JsonResult(1,laserService.pageTLasersAndPrinter(page,search,radio));
    }

    //确认打印，并修改laser表中打印状态
    @PostMapping("/lasers/updates")
    public JsonResult updatePrinter(@RequestBody OrderIdsAndPrinterNum orderIdsAndPrinterNum){
        UpdateWrapper<TLaser> updateWrapper = new UpdateWrapper<>();

        if (orderIdsAndPrinterNum.getPnum() != null) {
            updateWrapper.set("l_printer",orderIdsAndPrinterNum.getPnum());
        }else {
            List<TPrinter> printers = printerService.list();

            updateWrapper.set("l_printer", printers.get(new Random().nextInt(printers.size())).getPNum());
        }
        updateWrapper.set("l_status",1);

        orderIdsAndPrinterNum.getOrderids().forEach(item -> {
            updateWrapper.eq("l_no",item);
            updateWrapper.or();
        });

        laserService.update(updateWrapper);

        return new JsonResult(1,"打印成功");
    }
}
